﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Diagnostics;

/// <summary>
/// Description résumée de WebPartsRenderer
/// </summary>
/// 
namespace BusinessSite.Utility
{

    public sealed class WebPartsRenderer
    {
        public  const string VIEW_PRODUCTS = "?ipage=viewproduct";
        public  const string SUPPLIERS_LIST = "?ipage=supplierlist";
        public  const string ADD_PRODUCT = "?ipage=addproduct&id=0";
        public  const string ADD_CATEGORY = "?ipage=addcategory";
        public  const string ADD_SUPPLIER = "?ipage=addsupplier";

        public  const string NAV_STYLE_CLASS = "NavLeftTitle";
        public  const string MASTER_CONTENT_ID = "ctl00$PageContent$ctl00$";

	    #region Full lazy singleton pattern WebPartsRenderer

        WebPartsRenderer()
        {
        }

        public static WebPartsRenderer Instance
        {
            get
            {
                return Nested.instance;
            }
        }
        
        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }

            internal static readonly WebPartsRenderer instance = new WebPartsRenderer();
        }

	    #endregion

        public void HtmlRenderNavRight(Literal menu, Literal title)
        {
            StringBuilder rendererTitle = new StringBuilder();

            rendererTitle.Append("Main");

            title.Text = rendererTitle.ToString();

            StringBuilder rendererMenu = new StringBuilder();

            rendererMenu.AppendFormat("<table class=\"{0}\">", NAV_STYLE_CLASS);
            rendererMenu.AppendFormat("<tr>");
            rendererMenu.AppendFormat("<td style=\"width: 100px\">");
            rendererMenu.AppendFormat("<a href=\"{0}\">View products</a></td>", VIEW_PRODUCTS);
            rendererMenu.AppendFormat("</tr>");
            rendererMenu.AppendFormat("<tr>");
            rendererMenu.AppendFormat("<td style=\"width: 100px\">");
            rendererMenu.AppendFormat("<a href=\"{0}\">Suppliers list</a></td>", SUPPLIERS_LIST);
            rendererMenu.AppendFormat("</tr>");
            rendererMenu.AppendFormat("<tr>");
            rendererMenu.AppendFormat("<td style=\"width: 100px\">");
            rendererMenu.AppendFormat("</td>");
            rendererMenu.AppendFormat("</tr>");
            rendererMenu.AppendFormat("<tr>");
            rendererMenu.AppendFormat("<td style=\"width: 100px\">");
            rendererMenu.AppendFormat("<a href=\"{0}\">New product</a></td>",ADD_PRODUCT);
            rendererMenu.AppendFormat("</tr>");
            rendererMenu.AppendFormat("<tr>");
            rendererMenu.AppendFormat("<td style=\"width: 100px\">");
            rendererMenu.AppendFormat("<a href=\"{0}\">New supplier</a></td>", ADD_SUPPLIER);
            rendererMenu.AppendFormat("</tr>");
            rendererMenu.AppendFormat("<tr>");
            rendererMenu.AppendFormat("<td style=\"width: 100px\">");
            rendererMenu.AppendFormat("<a href=\"{0}\">New category</a></td>", ADD_CATEGORY);
            rendererMenu.AppendFormat("</tr>");
            rendererMenu.AppendFormat("</table>");

            menu.Text = rendererMenu.ToString();
        }

        public void HtmlErrorMessage(Literal error, ErrorType type)
        {
            switch (type)
            {
                case ErrorType.NotConnected:
                    error.Text = "Connexion problem, please check database server";
                    break;
                default:
                    error.Text = "";
                    break;
            }
        }
    }

}
